package com.proposition.exam.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.proposition.commons.exam.bean.ExamPaperSearchItem;
import com.proposition.commons.exam.bean.ExamSearchItem;
import com.proposition.commons.exam.result.Result;
import com.proposition.commons.exam.result.ResultEnum;
import com.proposition.commons.exam.result.ResultUtil;
import com.proposition.commons.exam.util.*;
import com.proposition.commons.exam.vo.ExamVO;
import com.proposition.commons.model.*;
import com.proposition.exam.mapper.ExamPaperMapper;
import com.proposition.exam.mapper.ExamVOMapper;
import com.proposition.exam.service.IServiceExamManage;
import com.proposition.support.security.CurrentUser;
import com.proposition.user.mapper.UserMapper;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URISyntaxException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: 陈玉林
 * @modifiedBy:
 * @date: Create In 16:36 2017/12/5
 * @description:
 */
@Service
@Transactional
public class IServiceExamManageImpl implements IServiceExamManage {
    @Autowired
    ExamVOMapper examVOMapper;
    @Autowired
    ExamPaperMapper examPaperMapper;

    @Autowired
    UserMapper userMapper;

    @Override
    public Page<ExamVO> selectAdminExamPage(Page page, ExamSearchItem searchItem) {
        page.setRecords(examVOMapper.selectAdminExamList(page, searchItem));
        return page;
    }

    @Override
    public Page<ExamVO> selectExamPage(Page<ExamVO> page, ExamSearchItem searchItem) {
        page.setRecords(examVOMapper.selectExamList(page, searchItem));
        return page;
    }

    @Override
    public Page<ExamVO> selectExamPaperPage(Page<ExamVO> page, ExamPaperSearchItem searchItem) {
        page.setRecords(examPaperMapper.selectExamList(page, searchItem));
        return page;
    }

    @Override
    public ModelAndView getExamDetail(String id) {
        Integer categoryId = Integer.valueOf(id.substring(0, 2));
        ModelAndView mv = null;
        String userIdImport = "";
        String userIdCheck1 = "";
        String userIdCheck2 = "";
        //问答题-腧穴
        if (categoryId == 1) {
            mv = new ModelAndView("exam/detail/detail-exam-acupoint");
            /*导入试题*/
            ExamAcupointOriginal exam1 = new ExamAcupointOriginal().selectOne(new EntityWrapper().eq("id", id));
            ExamAcupointCheck1 exam2 = new ExamAcupointCheck1().selectOne(new EntityWrapper().eq("id", id));
            ExamAcupoint exam3 = new ExamAcupoint().selectOne(new EntityWrapper().eq("id", id));

            ExamAcupoint exam4 = null;
            if (exam3.getCheckStatusId() > 2) {
                exam4 = new ExamAcupoint().selectOne(new EntityWrapper().eq("id", id).eq("check_status_id", exam3.getCheckStatusId()));
            }

            //导入用户
            userIdImport = exam3.getUserIdImport();
            //一审
            userIdCheck1 = exam3.getUserIdCheck1();
            //二审
            userIdCheck2 = exam3.getUserIdCheck2();

            mv.addObject("exam1", exam1);
            mv.addObject("exam2", exam2);
            mv.addObject("exam3", exam3);
            mv.addObject("exam4", exam4);

        }
        //穴位定位-儿科穴位
        else if (categoryId == 7) {
            mv = new ModelAndView("exam/detail/detail-exam-acupoint-location-pediatric");
            ExamAcupointPediatricOriginal exam1 = new ExamAcupointPediatricOriginal().selectOne(new EntityWrapper().eq("id", id));
            ExamAcupointPediatricCheck1 exam2 = new ExamAcupointPediatricCheck1().selectOne(new EntityWrapper().eq("id", id));
            ExamAcupointPediatric exam3 = new ExamAcupointPediatric().selectOne(new EntityWrapper().eq("id", id));

            ExamAcupointPediatric exam4 = null;
            if (exam3.getCheckStatusId() > 2) {
                exam4 = new ExamAcupointPediatric().selectOne(new EntityWrapper().eq("id", id).eq("check_status_id", exam3.getCheckStatusId()));
            }

            mv.addObject("exam1", exam1);
            mv.addObject("exam2", exam2);
            mv.addObject("exam3", exam3);
            mv.addObject("exam4", exam4);

            userIdImport = exam3.getUserIdImport();
            userIdCheck1 = exam3.getUserIdCheck1();
            userIdCheck2 = exam3.getUserIdCheck2();

            ExamPart part1 = new ExamPart();
            part1.setId(exam1.getPartIdPediatric());
            part1 = part1.selectById();

            ExamPart part2 = new ExamPart();
            if (exam2 != null) {
                part2.setId(exam2.getPartIdPediatric());
                part2 = part2.selectById();
            }

            ExamPart part3 = new ExamPart();
            part3.setId(exam3.getPartIdPediatric());
            part3 = part3.selectById();
            mv.addObject("part1", part1);
            mv.addObject("part2", part2);
            mv.addObject("part3", part3);
        }
        //穴位定位-成人穴位
        else if (categoryId == 8) {
            mv = new ModelAndView("exam/detail/detail-exam-acupoint-location-adult");
            ExamAcupointAdultOriginal exam1 = new ExamAcupointAdultOriginal().selectOne(new EntityWrapper().eq("id", id));
            ExamAcupointAdultCheck1 exam2 = new ExamAcupointAdultCheck1().selectOne(new EntityWrapper().eq("id", id));
            ExamAcupointAdult exam3 = new ExamAcupointAdult().selectOne(new EntityWrapper().eq("id", id));

            ExamAcupointAdult exam4 = null;
            if (exam3.getCheckStatusId() > 2) {
                exam4 = new ExamAcupointAdult().selectOne(new EntityWrapper().eq("id", id).eq("check_status_id", exam3.getCheckStatusId()));
            }

            mv.addObject("exam1", exam1);
            mv.addObject("exam2", exam2);
            mv.addObject("exam3", exam3);
            mv.addObject("exam4", exam4);

            userIdImport = exam3.getUserIdImport();
            userIdCheck1 = exam3.getUserIdCheck1();
            userIdCheck2 = exam3.getUserIdCheck2();

            ExamPart part1 = new ExamPart();
            part1.setId(exam1.getPartIdAdult());
            part1 = part1.selectById();

            ExamPart part2 = new ExamPart();
            if (exam2 != null) {
                part2.setId(exam2.getPartIdAdult());
                part2 = part2.selectById();
            }

            ExamPart part3 = new ExamPart();
            part3.setId(exam3.getPartIdAdult());
            part3 = part3.selectById();
            mv.addObject("part1", part1);
            mv.addObject("part2", part2);
            mv.addObject("part3", part3);
        }
        //10条答案
        else if (categoryId == 2 || categoryId == 3 || categoryId == 4 || categoryId == 5 || categoryId == 6 || categoryId == 9 || categoryId == 10 || categoryId == 11) {
            mv = new ModelAndView("exam/detail/detail-exam-answer10");
            ExamAnswer10Original exam1 = new ExamAnswer10Original().selectOne(new EntityWrapper().eq("id", id));
            ExamAnswer10Check1 exam2 = new ExamAnswer10Check1().selectOne(new EntityWrapper().eq("id", id));
            ExamAnswer10 exam3 = new ExamAnswer10().selectOne(new EntityWrapper().eq("id", id));

            ExamAnswer10 exam4 = null;
            if (exam3.getCheckStatusId() > 2) {
                exam4 = new ExamAnswer10().selectOne(new EntityWrapper().eq("id", id).eq("check_status_id", exam3.getCheckStatusId()));
            }

            //单项-成人手法时判断是否为五大类
            if (categoryId == 10) {
                Integer singlePloyFiveCategoryId = exam3.getSinglePloyFiveCategoryId();
                if (singlePloyFiveCategoryId > 0) {
                    ExamSinglePloyFiveCategory examSinglePloyFiveCategory = new ExamSinglePloyFiveCategory();
                    examSinglePloyFiveCategory.setId(singlePloyFiveCategoryId);
                    examSinglePloyFiveCategory = examSinglePloyFiveCategory.selectById();
                    mv.addObject("examSinglePloyFiveCategory", examSinglePloyFiveCategory);
                }
            }
            if (categoryId == 9) {
                Integer examCategorySinglePloyPediatricSubclassId = exam3.getExamCategorySinglePloyPediatricSubclassId();
                if (examCategorySinglePloyPediatricSubclassId > 0) {
                    ExamCategorySinglePloyPediatricSubclass examCategorySinglePloyPediatricSubclass = new ExamCategorySinglePloyPediatricSubclass();
                    examCategorySinglePloyPediatricSubclass.setId(examCategorySinglePloyPediatricSubclassId);
                    examCategorySinglePloyPediatricSubclass = examCategorySinglePloyPediatricSubclass.selectById();
                    mv.addObject("examCategorySinglePloyPediatricSubclass", examCategorySinglePloyPediatricSubclass);
                }
            }
            mv.addObject("exam1", exam1);
            mv.addObject("exam2", exam2);
            mv.addObject("exam3", exam3);
            mv.addObject("exam4", exam4);

            userIdImport = exam3.getUserIdImport();
            userIdCheck1 = exam3.getUserIdCheck1();
            userIdCheck2 = exam3.getUserIdCheck2();
        }
        //30条答案
        else if (categoryId == 12 || categoryId == 13 || categoryId == 14 || categoryId == 15) {
            mv = new ModelAndView("exam/detail/detail-exam-answer30");
            ExamAnswer30Original exam1 = new ExamAnswer30Original().selectOne(new EntityWrapper().eq("id", id));
            ExamAnswer30Check1 exam2 = new ExamAnswer30Check1().selectOne(new EntityWrapper().eq("id", id));
            ExamAnswer30 exam3 = new ExamAnswer30().selectOne(new EntityWrapper().eq("id", id));

            ExamAnswer30 exam4 = null;
            if (exam3.getCheckStatusId() > 2) {
                exam4 = new ExamAnswer30().selectOne(new EntityWrapper().eq("id", id).eq("check_status_id", exam3.getCheckStatusId()));
            }
            mv.addObject("exam1", exam1);
            mv.addObject("exam2", exam2);
            mv.addObject("exam3", exam3);
            mv.addObject("exam4", exam4);

            userIdImport = exam3.getUserIdImport();
            userIdCheck1 = exam3.getUserIdCheck1();
            userIdCheck2 = exam3.getUserIdCheck2();
        }

        User userImport = userMapper.findById(userIdImport);
        User userCheck1 = userMapper.findById(userIdCheck1);
        User userCheck2 = userMapper.findById(userIdCheck2);

        mv.addObject("userImport", userImport);
        mv.addObject("userCheck1", userCheck1);
        mv.addObject("userCheck2", userCheck2);
        return mv;
    }

    @Override
    public ModelAndView intoUpdatePage(String id) {
        Integer categoryId = Integer.valueOf(id.substring(0, 2));
        ModelAndView mv = null;
        if (categoryId == 1) {
            mv = new ModelAndView("exam/update/update-exam-acupoint");
            ExamAcupoint exam = new ExamAcupoint().selectOne(new EntityWrapper().eq("id", id));
            mv.addObject("exam", exam);
            mv.addObject("examParamWendaAcupoint", new ExamParamWendaAcupoint().selectOne(new EntityWrapper()));
        } else if (categoryId == 2 || categoryId == 3 || categoryId == 4 || categoryId == 5 || categoryId == 6) {
            mv = new ModelAndView("exam/update/update-exam-answer10-wenda");
            ExamAnswer10 exam = new ExamAnswer10().selectOne(new EntityWrapper().eq("id", id));
            mv.addObject("exam", exam);
            mv.addObject("examParamWendaFSENC", new ExamParamWendaFSENC().selectOne(new EntityWrapper()));
        } else if (categoryId == 7) {
            mv = new ModelAndView("exam/update/update-exam-acupoint-pediatric");
            ExamAcupointPediatric exam = new ExamAcupointPediatric().selectOne(new EntityWrapper().eq("id", id));
            mv.addObject("exam", exam);
            mv.addObject("examParamAcupointLocationPediatric", new ExamParamAcupointLocationPediatric().selectOne(new EntityWrapper()));

            mv.addObject("partList", new ExamPart().selectList(new EntityWrapper()));
        } else if (categoryId == 8) {
            mv = new ModelAndView("exam/update/update-exam-acupoint-adult");
            ExamAcupointAdult exam = new ExamAcupointAdult().selectOne(new EntityWrapper().eq("id", id));
            mv.addObject("exam", exam);
            mv.addObject("examParamAcupointLocationAdult", new ExamParamAcupointLocationAdult().selectOne(new EntityWrapper()));

            mv.addObject("partList", new ExamPart().selectList(new EntityWrapper()));
        } else if (categoryId == 9 || categoryId == 10 || categoryId == 11) {
            mv = new ModelAndView("exam/update/update-exam-answer10-single-ploy");
            ExamAnswer10 exam = new ExamAnswer10().selectOne(new EntityWrapper().eq("id", id));
            mv.addObject("exam", exam);
            mv.addObject("examParamSinglePloyCEC", new ExamParamSinglePloyCEC().selectOne(new EntityWrapper()));
        } else if (categoryId == 12 || categoryId == 13 || categoryId == 14 || categoryId == 15) {
            mv = new ModelAndView("exam/update/update-exam-answer30-complete_ploy");
            ExamAnswer30 exam = new ExamAnswer30().selectOne(new EntityWrapper().eq("id", id));
            mv.addObject("exam", exam);
            mv.addObject("examParamCompletePloyFSEN", new ExamParamCompletePloyFSEN().selectOne(new EntityWrapper()));
        }
        return mv;
    }

    @Override
    public Result updateExamAcupoint(String[] answer, Float[] answerScore, String[] answer1, Float[] answerScore1, ExamAcupoint exam, ExamAcupointCheck1 check1) {
        Timestamp time = new Timestamp(System.currentTimeMillis());
        exam.setUpdateTime(time);
        check1.setUpdateTime(time);
        String stem = ExamFieldVolidateUtil.getIllegalStem(exam.getStem());
        exam.setStem(stem);
        check1.setStem(stem);
        /*判断题干是否与已知试题冲突*/
        ExamAcupoint exist = exam.selectOne(new EntityWrapper().eq("stem", exam.getStem()));
        if (exist != null) {
            if (!exam.getId().equals(exist.getId())) {
                return new Result(ResultEnum.ILLEGAL_STEM_EXIST, null);
            }
        }
        if (answer != null) {
            for (int i = 0; i < answer.length ; i++) {
                switch (i+2) {
                    case 2:
                        exam.setLocation2(answer[i]);
                        check1.setLocation2(answer[i]);
                        break;
                    case 3:
                        exam.setLocation3(answer[i]);
                        check1.setLocation3(answer[i]);
                        break;
                    default:
                        break;
                }
            }
            for (int i = 0; i < answerScore.length; i++) {
                switch (i+2) {
                    case 2:
                        exam.setLocation2Score(answerScore[i]);
                        check1.setLocation2Score(answerScore[i]);
                        break;
                    case 3:
                        exam.setLocation3Score(answerScore[i]);
                        check1.setLocation3Score(answerScore[i]);
                        break;
                    default:
                        break;
                }
            }
        }

        if (answer1 != null) {
            for (int i = 0; i < answer1.length; i++) {
                switch (i + 2) {
                    case 2:
                        exam.setMayorCure2(answer1[i]);
                        check1.setMayorCure2(answer1[i]);
                        break;
                    case 3:
                        exam.setMayorCure3(answer1[i]);
                        check1.setMayorCure3(answer1[i]);
                        break;
                    case 4:
                        exam.setMayorCure4(answer1[i]);
                        check1.setMayorCure4(answer1[i]);
                        break;
                    case 5:
                        exam.setMayorCure5(answer1[i]);
                        check1.setMayorCure5(answer1[i]);
                        break;
                    case 6:
                        exam.setMayorCure6(answer1[i]);
                        check1.setMayorCure6(answer1[i]);
                        break;
                    case 7:
                        exam.setMayorCure7(answer1[i]);
                        check1.setMayorCure7(answer1[i]);
                        break;
                    case 8:
                        exam.setMayorCure8(answer1[i]);
                        check1.setMayorCure8(answer1[i]);
                        break;
                    default:
                        break;
                }
            }
            for (int i = 0; i < answerScore1.length; i++) {
                switch (i + 2) {
                    case 2:
                        exam.setMayorCure2Score(answerScore1[i]);
                        check1.setMayorCure2Score(answerScore1[i]);
                        break;
                    case 3:
                        exam.setMayorCure3Score(answerScore1[i]);
                        check1.setMayorCure3Score(answerScore1[i]);
                        break;
                    case 4:
                        exam.setMayorCure4Score(answerScore1[i]);
                        check1.setMayorCure4Score(answerScore1[i]);
                        break;
                    case 5:
                        exam.setMayorCure5Score(answerScore1[i]);
                        check1.setMayorCure5Score(answerScore1[i]);
                        break;
                    case 6:
                        exam.setMayorCure6Score(answerScore1[i]);
                        check1.setMayorCure6Score(answerScore1[i]);
                        break;
                    case 7:
                        exam.setMayorCure7Score(answerScore1[i]);
                        check1.setMayorCure7Score(answerScore1[i]);
                        break;
                    case 8:
                        exam.setMayorCure8Score(answerScore1[i]);
                        check1.setMayorCure8Score(answerScore1[i]);
                        break;
                    default:
                        break;
                }
            }
        }
        ExamAcupoint examTemp = exam.selectById();
        boolean permission = ExamManageUtil.isUpdateCheckOne("sx_ys", "sx_es", examTemp.getCheckStatusId());
        if (permission) {
            if (check1.selectById() != null) {
                check1.deleteById();
                check1.insert();
            } else {
                check1.insert();
            }
        }
        exam.deleteById();
        exam.setCheckStatusId(examTemp.getCheckStatusId());
        exam.setCategoryId(examTemp.getCategoryId());
        exam.setUpdateTime(time);
        exam.setUserIdImport(examTemp.getUserIdImport());
        exam.setUserIdCheck1(examTemp.getUserIdCheck1());
        exam.setUserIdCheck2(examTemp.getUserIdCheck2());
        exam.insert();
        return ResultUtil.sucess();
    }

    @Override
    public Result updateExamAcupointAdult(String[] answer, Float[] answerScore, ExamAcupointAdult exam, ExamAcupointAdultCheck1 check1) {
        Timestamp time = new Timestamp(System.currentTimeMillis());
        exam.setUpdateTime(time);
        check1.setUpdateTime(time);
        String stem = ExamFieldVolidateUtil.getIllegalStem(exam.getStem());
        exam.setStem(stem);
        check1.setStem(stem);
       /*判断题干是否与已知试题冲突*/
        ExamAcupointAdult exist = exam.selectOne(new EntityWrapper().eq("stem", exam.getStem()));
        if (exist != null) {
            if (!exam.getId().equals(exist.getId())) {
                return new Result(ResultEnum.ILLEGAL_STEM_EXIST, null);
            }
        }
        if (answer != null) {
            for (int i = 0; i < answer.length; i++) {
                switch (i + 2) {
                    case 2:
                        exam.setLocation2Adult(answer[i]);
                        check1.setLocation2Adult(answer[i]);
                        break;
                    case 3:
                        exam.setLocation3Adult(answer[i]);
                        check1.setLocation3Adult(answer[i]);
                        break;
                    default:
                        break;
                }
            }
            for (int i = 0; i < answerScore.length; i++) {
                switch (i + 2) {
                    case 2:
                        exam.setLocation2ScoreAdult(answerScore[i]);
                        check1.setLocation2ScoreAdult(answerScore[i]);
                        break;
                    case 3:
                        exam.setLocation3ScoreAdult(answerScore[i]);
                        check1.setLocation3ScoreAdult(answerScore[i]);
                        break;
                    default:
                        break;
                }
            }
        }
        ExamAcupointAdult examTemp = exam.selectById();
        boolean permission = ExamManageUtil.isUpdateCheckOne("crxw_ys", "crxw_es", examTemp.getCheckStatusId());
        if (permission) {
            if (check1.selectById() != null) {
                check1.deleteById();
                check1.insert();
            } else {
                check1.insert();
            }
        }
        exam.deleteById();
        exam.setCheckStatusId(examTemp.getCheckStatusId());
        exam.setCategoryId(examTemp.getCategoryId());
        exam.setUpdateTime(time);
        exam.setUserIdImport(examTemp.getUserIdImport());
        exam.setUserIdCheck1(examTemp.getUserIdCheck1());
        exam.setUserIdCheck2(examTemp.getUserIdCheck2());
        exam.insert();
        return ResultUtil.sucess();
    }

    @Override
    public Result updateExamAcupointPediatric(String[] answer, Float[] answerScore, ExamAcupointPediatric exam, ExamAcupointPediatricCheck1 check1) {
        Timestamp time = new Timestamp(System.currentTimeMillis());
        exam.setUpdateTime(time);
        check1.setUpdateTime(time);
        String stem = ExamFieldVolidateUtil.getIllegalStem(exam.getStem());
        exam.setStem(stem);
        check1.setStem(stem);
       /*判断题干是否与已知试题冲突*/
        ExamAcupointPediatric exist = exam.selectOne(new EntityWrapper().eq("stem", exam.getStem()));
        if (exist != null) {
            if (!exam.getId().equals(exist.getId())) {
                return new Result(ResultEnum.ILLEGAL_STEM_EXIST, null);
            }
        }
        if (answer != null) {
            for (int i = 0; i < answer.length; i++) {
                switch (i + 2) {
                    case 2:
                        exam.setLocation2Pediatric(answer[i]);
                        check1.setLocation2Pediatric(answer[i]);
                        break;
                    case 3:
                        exam.setLocation3Pediatric(answer[i]);
                        check1.setLocation3Pediatric(answer[i]);
                        break;
                    default:
                        break;
                }
            }
            for (int i = 0; i < answerScore.length; i++) {
                switch (i + 2) {
                    case 2:
                        exam.setLocation2ScorePediatric(answerScore[i]);
                        check1.setLocation2ScorePediatric(answerScore[i]);
                        break;
                    case 3:
                        exam.setLocation3ScorePediatric(answerScore[i]);
                        check1.setLocation3ScorePediatric(answerScore[i]);
                        break;
                    default:
                        break;
                }
            }
        }
        ExamAcupointPediatric examTemp = exam.selectById();
        boolean permission = ExamManageUtil.isUpdateCheckOne("ekxw_ys", "ekxw_es", examTemp.getCheckStatusId());
        if (permission) {
            if (check1.selectById() != null) {
                check1.deleteById();
                check1.insert();
            } else {
                check1.insert();
            }
        }
        exam.deleteById();
        exam.setCheckStatusId(examTemp.getCheckStatusId());
        exam.setCategoryId(examTemp.getCategoryId());
        exam.setUpdateTime(time);
        exam.setUserIdImport(examTemp.getUserIdImport());
        exam.setUserIdCheck1(examTemp.getUserIdCheck1());
        exam.setUserIdCheck2(examTemp.getUserIdCheck2());
        exam.insert();
        return ResultUtil.sucess();
    }

    @Override
    public Result updateExamWendaFSENC(String[] answer, Float[] answerScore, ExamAnswer10 exam, ExamAnswer10Check1 check1) {
        Timestamp time = new Timestamp(System.currentTimeMillis());
        exam.setUpdateTime(time);
        check1.setUpdateTime(time);
        String stem = ExamFieldVolidateUtil.getIllegalStem(exam.getStem());
        exam.setStem(stem);
        check1.setStem(stem);
       /*判断题干是否与已知试题冲突*/
        ExamAnswer10 exist = exam.selectOne(new EntityWrapper().eq("stem", exam.getStem()).eq("category_id", exam.getCategoryId()));
        if (exist != null) {
            if (!exam.getId().equals(exist.getId())) {
                return new Result(ResultEnum.ILLEGAL_STEM_EXIST, null);
            }
        }
        if (answer != null) {
            for (int i = 0; i < answer.length; i++) {
                switch (i + 2) {
                    case 2:
                        exam.setAnswer2(answer[i]);
                        check1.setAnswer2(answer[i]);
                        break;
                    case 3:
                        exam.setAnswer3(answer[i]);
                        check1.setAnswer3(answer[i]);
                        break;
                    case 4:
                        exam.setAnswer4(answer[i]);
                        check1.setAnswer4(answer[i]);
                        break;
                    case 5:
                        exam.setAnswer5(answer[i]);
                        check1.setAnswer5(answer[i]);
                        break;
                    case 6:
                        exam.setAnswer6(answer[i]);
                        check1.setAnswer6(answer[i]);
                        break;
                    case 7:
                        exam.setAnswer7(answer[i]);
                        check1.setAnswer7(answer[i]);
                        break;
                    case 8:
                        exam.setAnswer8(answer[i]);
                        check1.setAnswer8(answer[i]);
                        break;
                    case 9:
                        exam.setAnswer9(answer[i]);
                        check1.setAnswer9(answer[i]);
                        break;
                    case 10:
                        exam.setAnswer10(answer[i]);
                        check1.setAnswer10(answer[i]);
                        break;
                    default:
                        break;
                }
            }
            for (int i = 0; i < answerScore.length; i++) {
                switch (i + 2) {
                    case 2:
                        exam.setAnswer2Score(answerScore[i]);
                        check1.setAnswer2Score(answerScore[i]);
                        break;
                    case 3:
                        exam.setAnswer3Score(answerScore[i]);
                        check1.setAnswer3Score(answerScore[i]);
                        break;
                    case 4:
                        exam.setAnswer4Score(answerScore[i]);
                        check1.setAnswer4Score(answerScore[i]);
                        break;
                    case 5:
                        exam.setAnswer5Score(answerScore[i]);
                        check1.setAnswer5Score(answerScore[i]);
                        break;
                    case 6:
                        exam.setAnswer6Score(answerScore[i]);
                        check1.setAnswer6Score(answerScore[i]);
                        break;
                    case 7:
                        exam.setAnswer7Score(answerScore[i]);
                        check1.setAnswer7Score(answerScore[i]);
                        break;
                    case 8:
                        exam.setAnswer8Score(answerScore[i]);
                        check1.setAnswer8Score(answerScore[i]);
                        break;
                    case 9:
                        exam.setAnswer9Score(answerScore[i]);
                        check1.setAnswer9Score(answerScore[i]);
                        break;
                    case 10:
                        exam.setAnswer10Score(answerScore[i]);
                        check1.setAnswer10Score(answerScore[i]);
                        break;
                    default:
                        break;
                }
            }
        }
        ExamAnswer10 examTemp = exam.selectById();
        boolean permission = false;
        switch (examTemp.getCategoryId()) {
            case 2:
                permission = ExamManageUtil.isUpdateCheckOne("nk_ys", "nk_es", examTemp.getCheckStatusId());
                if (permission) {
                    if (check1.selectById() != null) {
                        check1.deleteById();
                        check1.insert();
                    } else {
                        check1.insert();
                    }
                }
                break;
            case 3:
                permission = ExamManageUtil.isUpdateCheckOne("sk_ys", "sk_es", examTemp.getCheckStatusId());
                if (permission) {
                    if (check1.selectById() != null) {
                        check1.deleteById();
                        check1.insert();
                    } else {
                        check1.insert();
                    }
                }
                break;
            case 4:
                permission = ExamManageUtil.isUpdateCheckOne("fk_ys", "fk_es", examTemp.getCheckStatusId());
                if (permission) {
                    if (check1.selectById() != null) {
                        check1.deleteById();
                        check1.insert();
                    } else {
                        check1.insert();
                    }
                }
                break;
            case 5:
                permission = ExamManageUtil.isUpdateCheckOne("ek_ys", "ek_es", examTemp.getCheckStatusId());
                if (permission) {
                    if (check1.selectById() != null) {
                        check1.deleteById();
                        check1.insert();
                    } else {
                        check1.insert();
                    }
                }
                break;
            case 6:
                permission = ExamManageUtil.isUpdateCheckOne("cz_ys", "cz_es", examTemp.getCheckStatusId());
                if (permission) {
                    if (check1.selectById() != null) {
                        check1.deleteById();
                        check1.insert();
                    } else {
                        check1.insert();
                    }
                }
                break;
            default:
                break;
        }
        exam.deleteById();
        exam.setCheckStatusId(examTemp.getCheckStatusId());
        exam.setCategoryId(examTemp.getCategoryId());
        exam.setUpdateTime(time);
        exam.setUserIdImport(examTemp.getUserIdImport());
        exam.setUserIdCheck1(examTemp.getUserIdCheck1());
        exam.setUserIdCheck2(examTemp.getUserIdCheck2());
        exam.insert();
        return ResultUtil.sucess();
    }

    @Override
    public Result updateExamSinglePloy(String[] answer, Float[] answerScore, ExamAnswer10 exam, ExamAnswer10Check1 check1) {
        Timestamp time = new Timestamp(System.currentTimeMillis());
        exam.setUpdateTime(time);
        check1.setUpdateTime(time);
        String stem = ExamFieldVolidateUtil.getIllegalStem(exam.getStem());
        exam.setStem(stem);
        check1.setStem(stem);
       /*判断题干是否与已知试题冲突*/
        ExamAnswer10 exist = exam.selectOne(new EntityWrapper().eq("stem", exam.getStem()).eq("category_id", exam.getCategoryId()));
        if (exist != null) {
            if (!exam.getId().equals(exist.getId())) {
                return new Result(ResultEnum.ILLEGAL_STEM_EXIST, null);
            }
        }
        if (answer != null) {
            for (int i = 0; i < answer.length; i++) {
                switch (i + 2) {
                    case 2:
                        exam.setAnswer2(answer[i]);
                        check1.setAnswer2(answer[i]);
                        break;
                    case 3:
                        exam.setAnswer3(answer[i]);
                        check1.setAnswer3(answer[i]);
                        break;
                    case 4:
                        exam.setAnswer4(answer[i]);
                        check1.setAnswer4(answer[i]);
                        break;
                    case 5:
                        exam.setAnswer5(answer[i]);
                        check1.setAnswer5(answer[i]);
                        break;
                    case 6:
                        exam.setAnswer6(answer[i]);
                        check1.setAnswer6(answer[i]);
                        break;
                    case 7:
                        exam.setAnswer7(answer[i]);
                        check1.setAnswer7(answer[i]);
                        break;
                    case 8:
                        exam.setAnswer8(answer[i]);
                        check1.setAnswer8(answer[i]);
                        break;
                    case 9:
                        exam.setAnswer9(answer[i]);
                        check1.setAnswer9(answer[i]);
                        break;
                    case 10:
                        exam.setAnswer10(answer[i]);
                        check1.setAnswer10(answer[i]);
                        break;
                    default:
                        break;
                }
            }
            for (int i = 0; i < answerScore.length; i++) {
                switch (i + 2) {
                    case 2:
                        exam.setAnswer2Score(answerScore[i]);
                        check1.setAnswer2Score(answerScore[i]);
                        break;
                    case 3:
                        exam.setAnswer3Score(answerScore[i]);
                        check1.setAnswer3Score(answerScore[i]);
                        break;
                    case 4:
                        exam.setAnswer4Score(answerScore[i]);
                        check1.setAnswer4Score(answerScore[i]);
                        break;
                    case 5:
                        exam.setAnswer5Score(answerScore[i]);
                        check1.setAnswer5Score(answerScore[i]);
                        break;
                    case 6:
                        exam.setAnswer6Score(answerScore[i]);
                        check1.setAnswer6Score(answerScore[i]);
                        break;
                    case 7:
                        exam.setAnswer7Score(answerScore[i]);
                        check1.setAnswer7Score(answerScore[i]);
                        break;
                    case 8:
                        exam.setAnswer8Score(answerScore[i]);
                        check1.setAnswer8Score(answerScore[i]);
                        break;
                    case 9:
                        exam.setAnswer9Score(answerScore[i]);
                        check1.setAnswer9Score(answerScore[i]);
                        break;
                    case 10:
                        exam.setAnswer10Score(answerScore[i]);
                        check1.setAnswer10Score(answerScore[i]);
                        break;
                    default:
                        break;
                }
            }
        }
        ExamAnswer10 examTemp = exam.selectById();
        boolean permission = false;
        switch (examTemp.getCategoryId()) {
            case 9:
                permission = ExamManageUtil.isUpdateCheckOne("eksf_ys", "eksf_es", examTemp.getCheckStatusId());
                if (permission) {
                    if (check1.selectById() != null) {
                        check1.deleteById();
                        check1.insert();
                    } else {
                        check1.insert();
                    }
                }
                break;
            case 10:
                permission = ExamManageUtil.isUpdateCheckOne("crsf_ys", "crsf_es", examTemp.getCheckStatusId());
                if (permission) {
                    if (check1.selectById() != null) {
                        check1.deleteById();
                        check1.insert();
                    } else {
                        check1.insert();
                    }
                }
                break;
            case 11:
                permission = ExamManageUtil.isUpdateCheckOne("czzd_ys", "czzd_es", examTemp.getCheckStatusId());
                if (permission) {
                    if (check1.selectById() != null) {
                        check1.deleteById();
                        check1.insert();
                    } else {
                        check1.insert();
                    }
                }
                break;
            case 16:
                permission = ExamManageUtil.isUpdateCheckOne("kghxw_ys", "kghxw_es", examTemp.getCheckStatusId());
                if (permission) {
                    if (check1.selectById() != null) {
                        check1.deleteById();
                        check1.insert();
                    } else {
                        check1.insert();
                    }
                }
                break;
            default:
                break;
        }
        exam.deleteById();
        exam.setCheckStatusId(examTemp.getCheckStatusId());
        exam.setCategoryId(examTemp.getCategoryId());
        exam.setUpdateTime(time);
        exam.setUserIdImport(examTemp.getUserIdImport());
        exam.setUserIdCheck1(examTemp.getUserIdCheck1());
        exam.setUserIdCheck2(examTemp.getUserIdCheck2());
        exam.insert();
        return ResultUtil.sucess();
    }

    @Override
    public Result updateExamCompletePloy(String[] answer, Float[] answerScore, ExamAnswer30 exam, ExamAnswer30Check1 check1) {
        Timestamp time = new Timestamp(System.currentTimeMillis());
        String stem = ExamFieldVolidateUtil.getIllegalStem(exam.getStem());
        exam.setStem(stem);
        check1.setStem(stem);
        check1.setUpdateTime(time);
        /*判断题干是否与已知试题冲突*/
        ExamAnswer30 exist = exam.selectOne(new EntityWrapper().eq("stem", exam.getStem()).eq("category_id", exam.getCategoryId()));
        if (exist != null) {
            if (!exam.getId().equals(exist.getId())) {
                return new Result(ResultEnum.ILLEGAL_STEM_EXIST, null);
            }
        }


        if (answer !=null) {
            for (int i = 0; i < answer.length; i++) {
                switch (i + 2) {
                    case 2:
                        exam.setAnswer2(answer[i]);
                        check1.setAnswer2(answer[i]);
                        break;
                    case 3:
                        exam.setAnswer3(answer[i]);
                        check1.setAnswer3(answer[i]);
                        break;
                    case 4:
                        exam.setAnswer4(answer[i]);
                        check1.setAnswer4(answer[i]);
                        break;
                    case 5:
                        exam.setAnswer5(answer[i]);
                        check1.setAnswer5(answer[i]);
                        break;
                    case 6:
                        exam.setAnswer6(answer[i]);
                        check1.setAnswer6(answer[i]);
                        break;
                    case 7:
                        exam.setAnswer7(answer[i]);
                        check1.setAnswer7(answer[i]);
                        break;
                    case 8:
                        exam.setAnswer8(answer[i]);
                        check1.setAnswer8(answer[i]);
                        break;
                    case 9:
                        exam.setAnswer9(answer[i]);
                        check1.setAnswer9(answer[i]);
                        break;
                    case 10:
                        exam.setAnswer10(answer[i]);
                        check1.setAnswer10(answer[i]);
                        break;
                    case 11:
                        exam.setAnswer11(answer[i]);
                        check1.setAnswer11(answer[i]);
                        break;
                    case 12:
                        exam.setAnswer12(answer[i]);
                        check1.setAnswer12(answer[i]);
                        break;
                    case 13:
                        exam.setAnswer13(answer[i]);
                        check1.setAnswer13(answer[i]);
                        break;
                    case 14:
                        exam.setAnswer14(answer[i]);
                        check1.setAnswer14(answer[i]);
                        break;
                    case 15:
                        exam.setAnswer15(answer[i]);
                        check1.setAnswer15(answer[i]);
                        break;
                    case 16:
                        exam.setAnswer16(answer[i]);
                        check1.setAnswer16(answer[i]);
                        break;
                    case 17:
                        exam.setAnswer17(answer[i]);
                        check1.setAnswer17(answer[i]);
                        break;
                    case 18:
                        exam.setAnswer18(answer[i]);
                        check1.setAnswer18(answer[i]);
                        break;
                    case 19:
                        exam.setAnswer19(answer[i]);
                        check1.setAnswer19(answer[i]);
                        break;
                    case 20:
                        exam.setAnswer20(answer[i]);
                        check1.setAnswer20(answer[i]);
                        break;
                    case 21:
                        exam.setAnswer21(answer[i]);
                        check1.setAnswer21(answer[i]);
                        break;
                    case 22:
                        exam.setAnswer22(answer[i]);
                        check1.setAnswer22(answer[i]);
                        break;
                    case 23:
                        exam.setAnswer23(answer[i]);
                        check1.setAnswer23(answer[i]);
                        break;
                    case 24:
                        exam.setAnswer24(answer[i]);
                        check1.setAnswer24(answer[i]);
                        break;
                    case 25:
                        exam.setAnswer25(answer[i]);
                        check1.setAnswer25(answer[i]);
                        break;
                    case 26:
                        exam.setAnswer26(answer[i]);
                        check1.setAnswer26(answer[i]);
                        break;
                    case 27:
                        exam.setAnswer27(answer[i]);
                        check1.setAnswer27(answer[i]);
                        break;
                    case 28:
                        exam.setAnswer28(answer[i]);
                        check1.setAnswer28(answer[i]);
                        break;
                    case 29:
                        exam.setAnswer29(answer[i]);
                        check1.setAnswer29(answer[i]);
                        break;
                    case 30:
                        exam.setAnswer30(answer[i]);
                        check1.setAnswer30(answer[i]);
                        break;
                    default:
                        break;
                }
            }
            for (int i = 0; i < answerScore.length; i++) {
                switch (i + 2) {
                    case 2:
                        exam.setAnswer2Score(answerScore[i]);
                        check1.setAnswer2Score(answerScore[i]);
                        break;
                    case 3:
                        exam.setAnswer3Score(answerScore[i]);
                        check1.setAnswer3Score(answerScore[i]);
                        break;
                    case 4:
                        exam.setAnswer4Score(answerScore[i]);
                        check1.setAnswer4Score(answerScore[i]);
                        break;
                    case 5:
                        exam.setAnswer5Score(answerScore[i]);
                        check1.setAnswer5Score(answerScore[i]);
                        break;
                    case 6:
                        exam.setAnswer6Score(answerScore[i]);
                        check1.setAnswer6Score(answerScore[i]);
                        break;
                    case 7:
                        exam.setAnswer7Score(answerScore[i]);
                        check1.setAnswer7Score(answerScore[i]);
                        break;
                    case 8:
                        exam.setAnswer8Score(answerScore[i]);
                        check1.setAnswer8Score(answerScore[i]);
                        break;
                    case 9:
                        exam.setAnswer9Score(answerScore[i]);
                        check1.setAnswer9Score(answerScore[i]);
                        break;
                    case 10:
                        exam.setAnswer10Score(answerScore[i]);
                        check1.setAnswer10Score(answerScore[i]);
                        break;
                    case 11:
                        exam.setAnswer11Score(answerScore[i]);
                        check1.setAnswer11Score(answerScore[i]);
                        break;
                    case 12:
                        exam.setAnswer12Score(answerScore[i]);
                        check1.setAnswer12Score(answerScore[i]);
                        break;
                    case 13:
                        exam.setAnswer13Score(answerScore[i]);
                        check1.setAnswer13Score(answerScore[i]);
                        break;
                    case 14:
                        exam.setAnswer14Score(answerScore[i]);
                        check1.setAnswer14Score(answerScore[i]);
                        break;
                    case 15:
                        exam.setAnswer15Score(answerScore[i]);
                        check1.setAnswer15Score(answerScore[i]);
                        break;
                    case 16:
                        exam.setAnswer16Score(answerScore[i]);
                        check1.setAnswer16Score(answerScore[i]);
                        break;
                    case 17:
                        exam.setAnswer17Score(answerScore[i]);
                        check1.setAnswer17Score(answerScore[i]);
                        break;
                    case 18:
                        exam.setAnswer18Score(answerScore[i]);
                        check1.setAnswer18Score(answerScore[i]);
                        break;
                    case 19:
                        exam.setAnswer19Score(answerScore[i]);
                        check1.setAnswer19Score(answerScore[i]);
                        break;
                    case 20:
                        exam.setAnswer20Score(answerScore[i]);
                        check1.setAnswer20Score(answerScore[i]);
                        break;
                    case 21:
                        exam.setAnswer21Score(answerScore[i]);
                        check1.setAnswer21Score(answerScore[i]);
                        break;
                    case 22:
                        exam.setAnswer22Score(answerScore[i]);
                        check1.setAnswer22Score(answerScore[i]);
                        break;
                    case 23:
                        exam.setAnswer23Score(answerScore[i]);
                        check1.setAnswer23Score(answerScore[i]);
                        break;
                    case 24:
                        exam.setAnswer24Score(answerScore[i]);
                        check1.setAnswer24Score(answerScore[i]);
                        break;
                    case 25:
                        exam.setAnswer25Score(answerScore[i]);
                        check1.setAnswer25Score(answerScore[i]);
                        break;
                    case 26:
                        exam.setAnswer26Score(answerScore[i]);
                        check1.setAnswer26Score(answerScore[i]);
                        break;
                    case 27:
                        exam.setAnswer27Score(answerScore[i]);
                        check1.setAnswer27Score(answerScore[i]);
                        break;
                    case 28:
                        exam.setAnswer28Score(answerScore[i]);
                        check1.setAnswer28Score(answerScore[i]);
                        break;
                    case 29:
                        exam.setAnswer29Score(answerScore[i]);
                        check1.setAnswer29Score(answerScore[i]);
                        break;
                    case 30:
                        exam.setAnswer30Score(answerScore[i]);
                        check1.setAnswer30Score(answerScore[i]);
                        break;
                    default:
                        break;
                }
            }
        }
        ExamAnswer30 examTemp = exam.selectById();
        boolean permission = false;
        switch (examTemp.getCategoryId()) {
            case 12:
                permission = ExamManageUtil.isUpdateCheckOne("eksf_ys", "eksf_es", examTemp.getCheckStatusId());
                if (permission) {
                    if (check1.selectById() != null) {
                        check1.deleteById();
                        check1.insert();
                    } else {
                        check1.insert();
                    }
                }
                break;
            case 13:
                permission = ExamManageUtil.isUpdateCheckOne("skct_ys", "skct_es", examTemp.getCheckStatusId());
                if (permission) {
                    if (check1.selectById() != null) {
                        check1.deleteById();
                        check1.insert();
                    } else {
                        check1.insert();
                    }
                }
                break;
            case 14:
                permission = ExamManageUtil.isUpdateCheckOne("fkct_ys", "fkct_es", examTemp.getCheckStatusId());
                if (permission) {
                    if (check1.selectById() != null) {
                        check1.deleteById();
                        check1.insert();
                    } else {
                        check1.insert();
                    }
                }
                break;
            case 15:
                permission = ExamManageUtil.isUpdateCheckOne("ekct_ys", "ekct_es", examTemp.getCheckStatusId());
                if (permission) {
                    if (check1.selectById() != null) {
                        check1.deleteById();
                        check1.insert();
                    } else {
                        check1.insert();
                    }
                }
                break;
            default:
                break;
        }
        exam.deleteById();
        exam.setCheckStatusId(examTemp.getCheckStatusId());
        exam.setCategoryId(examTemp.getCategoryId());
        exam.setUpdateTime(time);
        exam.setUserIdImport(examTemp.getUserIdImport());
        exam.setUserIdCheck1(examTemp.getUserIdCheck1());
        exam.setUserIdCheck2(examTemp.getUserIdCheck2());
        exam.insert();
        return ResultUtil.sucess();
    }

    /**
     * 审核状态为1、2时是一审
     * 审核状态为3、4时是二审
     * 如果是一审，先判断一审表里是否有试题，有的话更新，没有的话就添加
     * 原始original表任何时候都不做变动
     *
     * @param id            试题id
     * @param checkStatusId 审核状态id
     * @param checkRemark   审核不通过的备注
     * @return
     */
    @Override
    public Result checkExam(String id, Integer checkStatusId, String checkRemark) {
        Integer categoryId = Integer.valueOf(id.substring(0, 2));
        ExamManageUtil.volidateExamCheckPermission(categoryId, checkStatusId);
        String userIdCheck1 = "";
        String userIdCheck2 = "";
        if (checkRemark == null || checkRemark == "") {
            checkRemark = "";
        }

        //设置审核人id
        if (checkStatusId == 1 || checkStatusId == 2) {
            userIdCheck1 = CurrentUser.INSTANCE.getUserId();
        } else if (checkStatusId == 3 || checkStatusId == 4) {
            userIdCheck2 = CurrentUser.INSTANCE.getUserId();
        }
        if (categoryId == 1) {
            ExamAcupoint exam = new ExamAcupoint();
            exam.setId(id);
            exam = exam.selectById();
            exam.setCheckStatusId(checkStatusId);
            exam.setCheckRemark(checkRemark);
            if (userIdCheck1 != "") {
                exam.setUserIdCheck1(userIdCheck1);
            }
            if (userIdCheck2 != "") {
                exam.setUserIdCheck2(userIdCheck2);
            }
            exam.updateById();
            /*
                一审表里有试题，查询出来,然后进行更新
                没有的话就插入
             */
            if (checkStatusId == 1 || checkStatusId == 2) {
                ExamAcupointCheck1 examCheck1 = new ExamAcupointCheck1();
                examCheck1.setId(id);
                examCheck1 = examCheck1.selectById();
                if (examCheck1 != null) {
                    examCheck1.setCheckStatusId(checkStatusId);
                    examCheck1.setCheckRemark(checkRemark);
                    examCheck1.setUserIdCheck1(userIdCheck1);
                    examCheck1.updateById();
                } else {
                    examCheck1 = new ExamAcupointCheck1();
                    examCheck1 = ExamManageUtil.setValueIntoExamCheck1(exam, examCheck1);
                    examCheck1.insert();
                }
            }
        } else if (categoryId == 2 || categoryId == 3 || categoryId == 4 || categoryId == 5 || categoryId == 6 || categoryId == 9 || categoryId == 10 || categoryId == 11) {
            ExamAnswer10 exam = new ExamAnswer10();
            exam.setId(id);
            exam = exam.selectById();
            exam.setCheckStatusId(checkStatusId);
            exam.setCheckRemark(checkRemark);
            if (userIdCheck1 != "") {
                exam.setUserIdCheck1(userIdCheck1);
            }
            if (userIdCheck2 != "") {
                exam.setUserIdCheck2(userIdCheck2);
            }
            exam.updateById();

            if (checkStatusId == 1 || checkStatusId == 2) {
                ExamAnswer10Check1 examCheck1 = new ExamAnswer10Check1();
                examCheck1.setId(id);
                examCheck1 = examCheck1.selectById();
                if (examCheck1 != null) {
                    examCheck1.setCheckStatusId(checkStatusId);
                    examCheck1.setCheckRemark(checkRemark);
                    examCheck1.setUserIdCheck1(userIdCheck1);
                    examCheck1.updateById();
                } else {
                    examCheck1 = new ExamAnswer10Check1();
                    examCheck1 = ExamManageUtil.setValueIntoExamCheck1(exam, examCheck1);
                    examCheck1.insert();
                }
            }
        } else if (categoryId == 7) {
            ExamAcupointPediatric exam = new ExamAcupointPediatric();
            exam.setId(id);
            exam = exam.selectById();
            exam.setCheckStatusId(checkStatusId);
            exam.setCheckRemark(checkRemark);
            if (userIdCheck1 != "") {
                exam.setUserIdCheck1(userIdCheck1);
            }
            if (userIdCheck2 != "") {
                exam.setUserIdCheck2(userIdCheck2);
            }
            exam.updateById();

            if (checkStatusId == 1 || checkStatusId == 2) {
                ExamAcupointPediatricCheck1 examCheck1 = new ExamAcupointPediatricCheck1();
                examCheck1.setId(id);
                examCheck1 = examCheck1.selectById();
                if (examCheck1 != null) {
                    examCheck1.setCheckStatusId(checkStatusId);
                    examCheck1.setCheckRemark(checkRemark);
                    examCheck1.setUserIdCheck1(userIdCheck1);
                    examCheck1.updateById();
                } else {
                    examCheck1 = new ExamAcupointPediatricCheck1();
                    examCheck1 = ExamManageUtil.setValueIntoExamCheck1(exam, examCheck1);
                    examCheck1.insert();
                }
            }
        } else if (categoryId == 8) {
            ExamAcupointAdult exam = new ExamAcupointAdult();
            exam.setId(id);
            exam = exam.selectById();
            exam.setCheckStatusId(checkStatusId);
            exam.setCheckRemark(checkRemark);
            if (userIdCheck1 != "") {
                exam.setUserIdCheck1(userIdCheck1);
            }
            if (userIdCheck2 != "") {
                exam.setUserIdCheck2(userIdCheck2);
            }
            exam.updateById();

            if (checkStatusId == 1 || checkStatusId == 2) {
                ExamAcupointAdultCheck1 examCheck1 = new ExamAcupointAdultCheck1();
                examCheck1.setId(id);
                examCheck1 = examCheck1.selectById();
                if (examCheck1 != null) {
                    examCheck1.setCheckStatusId(checkStatusId);
                    examCheck1.setCheckRemark(checkRemark);
                    examCheck1.setUserIdCheck1(userIdCheck1);
                    examCheck1.updateById();
                } else {
                    examCheck1 = new ExamAcupointAdultCheck1();
                    examCheck1 = ExamManageUtil.setValueIntoExamCheck1(exam, examCheck1);
                    examCheck1.insert();
                }
            }
        } else if (categoryId == 12 || categoryId == 13 || categoryId == 14 || categoryId == 15) {
            ExamAnswer30 exam = new ExamAnswer30();
            exam.setId(id);
            exam = exam.selectById();
            exam.setCheckStatusId(checkStatusId);
            exam.setCheckRemark(checkRemark);
            if (userIdCheck1 != "") {
                exam.setUserIdCheck1(userIdCheck1);
            }
            if (userIdCheck2 != "") {
                exam.setUserIdCheck2(userIdCheck2);
            }
            exam.updateById();

            if (checkStatusId == 1 || checkStatusId == 2) {
                ExamAnswer30Check1 examCheck1 = new ExamAnswer30Check1();
                examCheck1.setId(id);
                examCheck1 = examCheck1.selectById();
                if (examCheck1 != null) {
                    examCheck1.setCheckStatusId(checkStatusId);
                    examCheck1.setCheckRemark(checkRemark);
                    examCheck1.setUserIdCheck1(userIdCheck1);
                    examCheck1.updateById();
                } else {
                    examCheck1 = new ExamAnswer30Check1();
                    examCheck1 = ExamManageUtil.setValueIntoExamCheck1(exam, examCheck1);
                    examCheck1.insert();
                }
            }
        }
        return ResultUtil.sucess();
    }

    /**
     * 若该试题已经组卷，不能撤销
     * 当撤回到一审通过时，将二审用户id清除；撤回到未审核时将一审用户id清除
     * 撤回到未审核时，应该同时删除一审表试题
     *
     * @param id
     * @param checkStatusId
     * @return
     */
    @Override
    public Result checkExamCancel(String id, Integer checkStatusId) {
        Result result = ResultUtil.sucess();
        //判断试题是否组卷
        ExamManageUtil.isInPaper(id);
        Integer categoryId = Integer.valueOf(id.substring(0, 2));
        ExamManageUtil.volidateExamCheckCancelPermission(categoryId, checkStatusId);

        /*验证权限*/
        if (categoryId == 1) {
            ExamAcupoint exam = new ExamAcupoint();
            exam.setId(id);
            exam = exam.selectById();
            exam.setCheckStatusId(checkStatusId);
            if (checkStatusId == 2) {
                exam.setUserIdCheck2("");
            } else if (checkStatusId == 0) {
                exam.setUserIdCheck1("");
                //删除一审表
                ExamAcupointCheck1 exam2 = new ExamAcupointCheck1();
                exam2.setId(id);
                exam2.deleteById();
            }
            //清除审核备注
            exam.setCheckRemark("");
            exam.updateById();
        } else if (categoryId == 2 || categoryId == 3 || categoryId == 4 || categoryId == 5 || categoryId == 6 || categoryId == 9 || categoryId == 10 || categoryId == 11) {
            ExamAnswer10 exam = new ExamAnswer10();
            exam.setId(id);
            exam = exam.selectById();
            exam.setCheckStatusId(checkStatusId);
            if (checkStatusId == 2) {
                exam.setUserIdCheck2("");
            } else if (checkStatusId == 0) {
                exam.setUserIdCheck1("");
                //删除一审表
                ExamAnswer10Check1 exam2 = new ExamAnswer10Check1();
                exam2.setId(id);
                exam2.deleteById();
            }
            exam.setCheckRemark("");
            exam.updateById();

        } else if (categoryId == 7) {
            ExamAcupointPediatric exam = new ExamAcupointPediatric();
            exam.setId(id);
            exam = exam.selectById();
            exam.setCheckStatusId(checkStatusId);
            if (checkStatusId == 2) {
                exam.setUserIdCheck2("");
            } else if (checkStatusId == 0) {
                exam.setUserIdCheck1("");

                //删除一审表
                ExamAcupointPediatricCheck1 exam2 = new ExamAcupointPediatricCheck1();
                exam2.setId(id);
                exam2.deleteById();
            }
            exam.setCheckRemark("");
            exam.updateById();
        } else if (categoryId == 8) {
            ExamAcupointAdult exam = new ExamAcupointAdult();
            exam.setId(id);
            exam = exam.selectById();
            exam.setCheckStatusId(checkStatusId);
            if (checkStatusId == 2) {
                exam.setUserIdCheck2("");
            } else if (checkStatusId == 0) {
                exam.setUserIdCheck1("");

                //删除一审表
                ExamAcupointAdultCheck1 exam2 = new ExamAcupointAdultCheck1();
                exam2.setId(id);
                exam2.deleteById();
            }
            exam.setCheckRemark("");
            exam.updateById();
        } else if (categoryId == 12 || categoryId == 13 || categoryId == 14 || categoryId == 15) {
            ExamAnswer30 exam = new ExamAnswer30();
            exam.setId(id);
            exam = exam.selectById();
            exam.setCheckStatusId(checkStatusId);
            if (checkStatusId == 2) {
                exam.setUserIdCheck2("");
            } else if (checkStatusId == 0) {
                exam.setUserIdCheck1("");

                //删除一审表
                ExamAnswer30Check1 exam2 = new ExamAnswer30Check1();
                exam2.setId(id);
                exam2.deleteById();
            }
            exam.setCheckRemark("");
            exam.updateById();
        }
        return result;
    }

    @Override
    public Result deleteExam(String id) {
        String deleteExamPermission = "stsc";
        ExamUtil.volidateAuthorization(deleteExamPermission);
        Integer categoryId = Integer.valueOf(id.substring(0, 2));
        if (categoryId == 1) {
            ExamAcupointOriginal exam1 = new ExamAcupointOriginal();
            ExamAcupointCheck1 exam2 = new ExamAcupointCheck1();
            ExamAcupoint exam3 = new ExamAcupoint();
            exam1.setId(id);
            exam2.setId(id);
            exam3.setId(id);

            exam1.deleteById();
            //判断一审表里是否有试题，有再进行删除
            if (exam2.selectById() != null) {
                exam2.deleteById();
            }
            exam3.deleteById();
        } else if (categoryId == 2 || categoryId == 3 || categoryId == 4 || categoryId == 5 || categoryId == 6 || categoryId == 9 || categoryId == 10 || categoryId == 11) {
            ExamAnswer10Original exam1 = new ExamAnswer10Original();
            ExamAnswer10Check1 exam2 = new ExamAnswer10Check1();
            ExamAnswer10 exam3 = new ExamAnswer10();
            exam1.setId(id);
            exam2.setId(id);
            exam3.setId(id);

            exam1.deleteById();
            //判断一审表里是否有试题，有再进行删除
            if (exam2.selectById() != null) {
                exam2.deleteById();
            }
            exam3.deleteById();
        } else if (categoryId == 7) {
            ExamAcupointPediatricOriginal exam1 = new ExamAcupointPediatricOriginal();
            ExamAcupointPediatricCheck1 exam2 = new ExamAcupointPediatricCheck1();
            ExamAcupointPediatric exam3 = new ExamAcupointPediatric();
            exam1.setId(id);
            exam2.setId(id);
            exam3.setId(id);

            exam1.deleteById();
            //判断一审表里是否有试题，有再进行删除
            if (exam2.selectById() != null) {
                exam2.deleteById();
            }
            exam3.deleteById();
        } else if (categoryId == 8) {
            ExamAcupointAdultOriginal exam1 = new ExamAcupointAdultOriginal();
            ExamAcupointAdultCheck1 exam2 = new ExamAcupointAdultCheck1();
            ExamAcupointAdult exam3 = new ExamAcupointAdult();
            exam1.setId(id);
            exam2.setId(id);
            exam3.setId(id);

            exam1.deleteById();
            //判断一审表里是否有试题，有再进行删除
            if (exam2.selectById() != null) {
                exam2.deleteById();
            }
            exam3.deleteById();
        } else if (categoryId == 12 || categoryId == 13 || categoryId == 14 || categoryId == 15) {
            ExamAnswer30Original exam1 = new ExamAnswer30Original();
            ExamAnswer30Check1 exam2 = new ExamAnswer30Check1();
            ExamAnswer30 exam3 = new ExamAnswer30();
            exam1.setId(id);
            exam2.setId(id);
            exam3.setId(id);

            exam1.deleteById();
            //判断一审表里是否有试题，有再进行删除
            if (exam2.selectById() != null) {
                exam2.deleteById();
            }
            exam3.deleteById();
        }
        return ResultUtil.sucess();
    }

    @Override
    public void writeExamToWord(String[] checkedId, HttpServletRequest request, HttpServletResponse response) throws IOException, TemplateException, URISyntaxException {
        if (checkedId != null) {
            String outFilePath = "";
            Integer categoryId = Integer.valueOf(checkedId[0].substring(0, 2));
            if (categoryId == 1) {
                //模板名
                String templateName = "exam-export-template-wenda-acupoint.ftl";
                int examCount = 0;
                Map<String, Object> dataMap = new HashMap<>();
                List<Map<String, Object>> examList = new ArrayList<>();
                for (String id : checkedId) {
                    Map<String, Object> map = new HashMap<>();
                    examCount++;
                    ExamAcupoint exam = new ExamAcupoint();
                    exam.setId(id);
                    exam = exam.selectById();
                    //题号
                    map.put("examCount", examCount);
                    //试题
                    map.put("exam", exam);
                    examList.add(map);
                    dataMap.put("examList", examList);

                    //3.把填充完成的xml写入到docx中
                    ExamExportUtil xtd = new ExamExportUtil();
                    outFilePath = xtd.outDocx(templateName, dataMap, request);
                }
            } else if (categoryId == 2 || categoryId == 3 || categoryId == 4 || categoryId == 5 || categoryId == 6 || categoryId == 9 || categoryId == 10 || categoryId == 11) {
                //模板名
                String templateName = "exam-export-template-answer10.ftl";
                int examCount = 0;
                Map<String, Object> dataMap = new HashMap<>();
                dataMap.put("category", ExamImportUtil.getTemplateCategoryName(categoryId));
                List<Map<String, Object>> examList = new ArrayList<>();
                for (String id : checkedId) {
                    Map<String, Object> map = new HashMap<>();
                    examCount++;
                    ExamAnswer10 exam = new ExamAnswer10();
                    exam.setId(id);
                    exam = exam.selectById();

                    //五大类
                    ExamSinglePloyFiveCategory fiveCategory = new ExamSinglePloyFiveCategory();
                    if (exam.getSinglePloyFiveCategoryId() == 0) {
                        map.put("fiveCategory", "");
                    } else {
                        fiveCategory.setId(exam.getSinglePloyFiveCategoryId());
                        fiveCategory = fiveCategory.selectById();
                        map.put("fiveCategory", fiveCategory.getName());
                    }

                    //题号
                    map.put("examCount", examCount);
                    //试题
                    map.put("exam", exam);
                    examList.add(map);
                    dataMap.put("examList", examList);


                    //3.把填充完成的xml写入到docx中
                    ExamExportUtil xtd = new ExamExportUtil();
                    outFilePath = xtd.outDocx(templateName, dataMap, request);

                }
            } else if (categoryId == 7) {
                String templateName = "exam-export-template-acupoint-location-pediatric.ftl";
                int examCount = 0;
                Map<String, Object> dataMap = new HashMap<>();
                dataMap.put("category", ExamImportUtil.getTemplateCategoryName(categoryId));
                List<Map<String, Object>> examList = new ArrayList<>();
                for (String id : checkedId) {
                    Map<String, Object> map = new HashMap<>();
                    examCount++;
                    ExamAcupointPediatric exam = new ExamAcupointPediatric();
                    exam.setId(id);
                    exam = exam.selectById();
                    //部位
                    ExamPart examPart = new ExamPart();
                    examPart.setId(exam.getPartIdPediatric());
                    map.put("part", examPart.selectById().getPart());
                    //题号
                    map.put("examCount", examCount);
                    //试题
                    map.put("exam", exam);
                    examList.add(map);
                    dataMap.put("examList", examList);


                    //3.把填充完成的xml写入到docx中
                    ExamExportUtil xtd = new ExamExportUtil();
                    outFilePath = xtd.outDocx(templateName, dataMap, request);
                }
            } else if (categoryId == 8) {
                String templateName = "exam-export-template-acupoint-location-adult.ftl";
                int examCount = 0;
                Map<String, Object> dataMap = new HashMap<>();
                dataMap.put("category", ExamImportUtil.getTemplateCategoryName(categoryId));
                List<Map<String, Object>> examList = new ArrayList<>();
                for (String id : checkedId) {
                    Map<String, Object> map = new HashMap<>();
                    examCount++;
                    ExamAcupointAdult exam = new ExamAcupointAdult();
                    exam.setId(id);
                    exam = exam.selectById();
                    //部位


                    ExamPart examPart = new ExamPart();
                    examPart.setId(exam.getPartIdAdult());
                    map.put("part", examPart.selectById().getPart());
                    //题号
                    map.put("examCount", examCount);
                    //试题
                    map.put("exam", exam);
                    examList.add(map);
                    dataMap.put("examList", examList);

                    //3.把填充完成的xml写入到docx中
                    ExamExportUtil xtd = new ExamExportUtil();
                    outFilePath = xtd.outDocx(templateName, dataMap, request);
                }
            } else if (categoryId == 12 || categoryId == 13 || categoryId == 14 || categoryId == 15) {
                //模板名
                String templateName = "exam-export-template-answer30.ftl";
                int examCount = 0;
                Map<String, Object> dataMap = new HashMap<>();
                dataMap.put("category", ExamImportUtil.getTemplateCategoryName(categoryId));
                List<Map<String, Object>> examList = new ArrayList<>();
                for (String id : checkedId) {
                    Map<String, Object> map = new HashMap<>();
                    examCount++;
                    ExamAnswer30 exam = new ExamAnswer30();
                    exam.setId(id);
                    exam = exam.selectById();
                    //题号
                    map.put("examCount", examCount);
                    //试题
                    map.put("exam", exam);
                    examList.add(map);
                    dataMap.put("examList", examList);

                    //3.把填充完成的xml写入到docx中
                    ExamExportUtil xtd = new ExamExportUtil();
                    outFilePath = xtd.outDocx(templateName, dataMap, request);
                }
            }
            InputStream fis = null;
            OutputStream os = null;
            try {
                fis = new BufferedInputStream(new FileInputStream(outFilePath));
                byte[] buffer = new byte[fis.available()];
                fis.read(buffer);
                // 清空response
                response.reset();
                response.setContentType("application/octet-stream");
                response.addHeader("Content-Disposition", "attachment; filename=\"" + new String("试题".getBytes("GB2312"), "ISO-8859-1") + ".docx\"");
                os = new BufferedOutputStream(response.getOutputStream());
                os.write(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (os != null) {
                        os.close();
                    }
                    if (fis != null) {
                        fis.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public Result getExamById(String id) {
        Result result = ResultUtil.sucess();
        Integer categoryId = Integer.valueOf(id.substring(0, 2));
        if (categoryId == 1) {
            ExamAcupoint exam = new ExamAcupoint();
            exam.setId(id);
            exam = exam.selectById();
            result.setData(exam);
        } else if (categoryId == 2 || categoryId == 3 || categoryId == 4 || categoryId == 5 || categoryId == 6 || categoryId == 9 || categoryId == 10 || categoryId == 11) {
            ExamAnswer10 exam = new ExamAnswer10();
            exam.setId(id);
            exam = exam.selectById();
            result.setData(exam);
        } else if (categoryId == 7) {
            ExamAcupointPediatric exam = new ExamAcupointPediatric();
            exam.setId(id);
            exam = exam.selectById();
            result.setData(exam);
        } else if (categoryId == 8) {
            ExamAcupointAdult exam = new ExamAcupointAdult();
            exam.setId(id);
            exam = exam.selectById();
            result.setData(exam);
        } else if (categoryId == 12 || categoryId == 13 || categoryId == 14 || categoryId == 15) {
            ExamAnswer30 exam = new ExamAnswer30();
            exam.setId(id);
            exam = exam.selectById();
            result.setData(exam);
        }
        return result;
    }

    /**
     * 根据试题id截取试题分类，判断是否市成人穴位；是-进行下一步，不是返回结果
     * 判断是否为客观化
     * 1 试题完成审后，才能进行设置
     * 2 是客观化，提示已经是客观化试题
     * 3 非客观化，是否为成人穴位；是-改为客观化，否-提示非成人穴位试题
     *
     * @param id
     * @return
     */
    @Override
    public Result setToObjective(String id) {
        ExamManageUtil.isInPaper(id);
        //验证权限
        ExamUtil.volidateAuthorization("kghsz");
        ExamAcupointAdult exam = new ExamAcupointAdult();
        exam.setId(id);
        exam = exam.selectById();
        exam.setCategoryId(16);
        exam.updateById();
        return ResultUtil.sucess();
    }

    /**
     * 1 判断categoryId不是16，就不是穴位定位-客观化
     *
     * @param id
     * @return
     */
    @Override
    public Result cancelObjective(String id) {
        ExamManageUtil.isInPaper(id);
        //验证权限
        String deleteExamPermission = "kghsz";
        ExamUtil.volidateAuthorization(deleteExamPermission);
        ExamAcupointAdult exam = new ExamAcupointAdult();
        exam.setId(id);
        exam = exam.selectById();
        exam.setCategoryId(8);
        exam.updateById();
        return ResultUtil.sucess();
    }

}
